#region Header

// MS codename Velocity Distributed Cache Extensions.
// (c) 2009

#endregion


#region Usings

using System;

using Caching.Extensions.Core;

using Rhino.Mocks;

#endregion


namespace Caching.Extension.Core.Tests.Utils
{
    /// <summary>
    /// Base class for SmartCache notification events testing
    /// </summary>
    public abstract class CacheNotificationTestsBase : SmartCacheTestsBase
    {
        #region Data

        protected const string CacheData = "1";
        private MockRepository m_repository;
        private ISmartCacheEventSink m_eventSink;

        protected MockRepository MockRepo
        {
            get { return m_repository; }
        }

        protected ISmartCacheEventSink EventSink
        {
            get { return m_eventSink; }
        }

        #endregion


        protected override void PostSetUp()
        {
            base.PostSetUp();

            m_repository = new MockRepository();
            m_eventSink = MockRepo.StrictMock<ISmartCacheEventSink>();
        }


        protected override void PreTearDown()
        {
            m_eventSink = null;
            m_repository = null;

            base.PreTearDown();
        }


        /// <summary>
        /// Set expectation on item operation event
        /// </summary>
        /// <param name="itemOperation"></param>
        /// <param name="cacheKey"></param>
        /// <param name="msg"></param>
        protected void ExpectItemOp(SmartCacheItemOperation itemOperation, SmartCacheKey cacheKey, string msg)
        {
            EventSink.RemoteCacheItemOperation(
                Arg<object>.Is.Anything,
                Arg<SmartCacheRemoteItemNotification>.Matches(n => n.Operation == itemOperation && n.Key == cacheKey)
                );
            LastCall.Message(msg);
        }


        /// <summary>
        /// Set expectation on item operation event
        /// </summary>
        /// <param name="itemOperation"></param>
        /// <param name="cacheKey"></param>
        /// <param name="msg"></param>
        protected void ExpectLocalItemOp(LocalCacheItemOperation itemOperation, SmartCacheKey cacheKey, string msg)
        {
            EventSink.LocalCacheItemNotification(Arg<object>.Is.Anything,
                Arg<SmartCacheLocalItemNotification>.Matches(n => n.Operation == itemOperation && n.Key == cacheKey)
                );
            LastCall.Message(msg);
        }


        protected void Subscribe()
        {
            TestCache.NotificationEvents.Subscribe(EventSink);
        }


        protected void ExpectRegionOp(string regionName, SmartCacheRegionOperation op, string msg)
        {
            EventSink.RegionOperation(Arg<object>.Is.Anything,
                Arg<SmartCacheRegionNotification>.Matches(n => n.RegionName == regionName && n.Operation == op));
            LastCall.Message(msg);
        }
    }
}